package com.car.tx;

import java.util.Map;
import java.util.Random;

import org.apache.log4j.Logger;

import com.car.common.DefaultConfig;
import com.car.common.model.Prop;
import com.car.common.model.PropRecord;
import com.car.common.model.Sign;
import com.car.common.model.Users;
import com.car.common.model.VipRecord;
import com.google.common.collect.Maps;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.utils.DateUtil;
import com.utils.PicUtil;
import com.utils.RongCloudUtil;
import com.utils.StringUtil;
import com.utils.Toolkit;

public class UserService {
	
	public static UserService one = new UserService();
	
	private static Logger logger = Logger.getLogger(UserService.class);
	
	/**
	 * 签到
	 * @param user_id
	 */
	@Before(Tx.class)
	public Map<String,Object> sign(Long user_id) {
		String nowString = DateUtil.getStringDate();
		
		Sign one = Sign.dao.findFirst("select * from sign where real_day = '" + nowString +"' and user_id = " + user_id + "  order by `index` desc limit 1 ");
		if (one == null) {
			one = Sign.dao.findFirst("select * from sign where user_id = " + user_id + " order by id desc limit 1 ");
			if (one == null || (System.currentTimeMillis()/1000L - 2 * 24 * 60 * 60L) >  one.getCreatetime() ) {
				one = new Sign();
				one.setUserId(user_id.intValue());
				one.setRealDay(nowString);
				one.setSignDay(nowString);
				one.setStatus(1);
				one.setIndex(1);
				one.setCreatetime(System.currentTimeMillis()/1000L);
				one.save();
				return null;
			} else {
				if (one.getIndex() != 7) {
					Sign newOne = new Sign();
					newOne.setUserId(user_id.intValue());
					newOne.setRealDay(nowString);
					newOne.setSignDay(one.getSignDay());
					newOne.setStatus(1);
					newOne.setIndex(one.getIndex() + 1);
					newOne.setCreatetime(System.currentTimeMillis()/1000L);
					newOne.save();
					if (one.getIndex() + 1 ==7 ) {
						// TODO 送礼物
						Prop prop = Prop.dao.findFirst("select id,name,icon,hot_val from prop where type = 1 order by id desc limit 1 ");
						if (prop != null) {
							Users users = Users.dao.findById(user_id);
							PropRecord onerec = new PropRecord();
							onerec.set("prop_id", 			prop.get("id"));
							onerec.set("hot_val", 			prop.get("hot_val"));
							onerec.set("user_id", 			user_id);
							onerec.set("nick_name", 		StringUtil.decode(users.getNickName()));
							onerec.set("user_header", 		users.getUserHeader());

							onerec.set("target_user_id", 	0);
							onerec.set("source",  			"sign");
							onerec.set("status",  			1);
							onerec.set("createtime",  		System.currentTimeMillis() /1000L);
							onerec.save();
							Map<String,Object> result = Maps.newHashMap();
							result.put("id", prop.get("id"));
							result.put("name", prop.get("name"));
							result.put("icon",  PicUtil.QINIU_DIR + prop.get("icon"));
							return result;
						}
					} 
				} 
			}
		}
		return null;
		/*
		user_id
		real_day
		sign_day
		createtime
		updatetime
		status
		index
		 */
	}

	/**
	 * 送礼物 TODO 未完成
	 * @param user_id
	 * @param target_user_id
	 * @param prop
	 */
	@Before(Tx.class)
	public int send_gift(Long user_id, int target_user_id, Prop prop,int num) {
		Users users = Users.dao.findById(user_id);
		// 送礼物扣费
		PropRecord one = new PropRecord();
		one.set("prop_id", 			prop.get("id"));
		one.set("hot_val", 			prop.get("hot_val"));
		one.set("user_id", 			user_id);
		one.set("nick_name", 		StringUtil.empty(users.getNickName())? "" : StringUtil.decode(users.getNickName()) );
		one.set("user_header", 		users.getUserHeader());
		one.set("num",  			num);
		one.set("target_user_id", 	target_user_id);
		one.set("source",  			"buy");
		one.set("status",  			1);
		one.set("createtime",  		System.currentTimeMillis() /1000L);
		int times = 0;
		if (one.save()) {
			if(prop.getType() == 0) {
				times ++;
				Db.update("update users set hot_val = hot_val - " + (prop.getInt("hot_val") * num) + ",credit = credit + 1 where id = "+ user_id);
				
				int rec_hot_value = prop.getInt("hot_val") * num * prop.getInt("rebate") / 100;
				// 女方
				Db.update("update users set free_hot = free_hot + " + rec_hot_value + ",credit = credit + 1 where id = "+ target_user_id);

//				RongCloudUtil.sendTxt("141", String.valueOf(target_user_id), "新消息，收到 " + StringUtil.decode(users.getNickName()) +"发的礼物。" );
				// 礼物送出
				// 女方魅力 +1 
				// 男方信用 +1
				// 参与抽奖
				// 10% 的概率 2倍
				/*
				int random = new Random().nextInt(12); 
				if (random == 6) {
					times  = 2 ;
				}
				if (times > 0) {
					// TODO 多个礼物 
					Db.update("update users set consume_hot_val = consume_hot_val + " + (times * prop.getInt("hot_val")) + ", hot_val = hot_val + " + (times * prop.getInt("hot_val")) + " where id = "+ user_id);
				}
				*/

			} 
		}
		return times;
	}
}
